package com.elinkway.infinitemovies.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.elinkway.infinitemovies.adapter.SubscriptionContentListAdapter;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.async.RequestSubscriptionAlbumTask;
import com.elinkway.infinitemovies.bean.PlayData;
import com.elinkway.infinitemovies.bean.SubscriptionAlbum;
import com.elinkway.infinitemovies.ui.activity.play.PlayActivitySelfOwn;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.Utils;
import com.elinkway.infinitemovies.view.CircleImageView;
import com.elinkway.infinitemovies.view.PublicLoadLayout;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by zhaopeipei on 2016/9/8.
 */
public class SubscriptionContentFragment extends BaseFragment {

    public static final String TAG = "SubscriptionContentFragment";

    private static final String HEAD_IMAGE = "head_image";

    private PublicLoadLayout root;

    private PullToRefreshListView mListView;

    private SubscriptionContentListAdapter mAdapter;

    private RequestSubscriptionAlbumTask mRequestSubscriptionAlbumTask;

    private SubscriptionAlbum mSubscriptionAlbum;

    private int mCurrentPage = 1;

    private TextView mTitle;

    private CircleImageView mHeadImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        root = UIs.createPage(getActivity(), R.layout.subscription_main);
        root.loading(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mListView = (PullToRefreshListView) root
                .findViewById(R.id.prlv_subscription_info);
        mTitle = (TextView) root.findViewById(R.id.subscription_title);
        mHeadImageView = (CircleImageView) root.findViewById(R.id.subscription_header_icon_iv);
        if (mSubscriptionAlbum == null) {
            mRequestSubscriptionAlbumTask = new RequestSubscriptionAlbumTask(getActivity(),
                    "318778", "0", "10", false);
            mRequestSubscriptionAlbumTask.setOnListener(new RequestResultListener<SubscriptionAlbum>() {
                @Override
                public boolean onRequestFailed() {
                    return false;
                }

                @Override
                public void onRequestSuccess(int updateId, SubscriptionAlbum result) {
                    if (mAdapter == null) {
                        mSubscriptionAlbum = result;
                        mAdapter = new SubscriptionContentListAdapter(getActivity(), result);
                        mListView.setAdapter(mAdapter);
                    } else {
                        if (result.isLoadMore()) {
                            mAdapter.addItemData(result);
                        } else {
                            mAdapter.setItemData(result);
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                    if (result.getVideoList().size() == MoviesConstant.TOPIC_REQUEST_SIZE) {
                        mCurrentPage += 1;
                    } else {
//                        setNoMore();
                    }
                    mListView.onRefreshComplete();
                    mTitle.setText(result.getName());
                    ImageLoader.getInstance().displayImage(getActivity().getIntent().getStringExtra(HEAD_IMAGE), mHeadImageView);
                    root.finish();
                }

                @Override
                public void onPreRequest() {

                }
            });
            mRequestSubscriptionAlbumTask.start();
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if(mSubscriptionAlbum == null || mSubscriptionAlbum.getVideoList() == null ||mSubscriptionAlbum.getVideoList().size() < 1){
                    return;
                }
                PlayData playData = new PlayData(mSubscriptionAlbum, mSubscriptionAlbum.getVideoList().get(position).getPorder());
                Intent intent = new Intent(getActivity(), PlayActivitySelfOwn.class);
                intent.putExtra("mIsHalfScreen", true);
                Bundle b = new Bundle();
                b.putSerializable(Utils.PLAY_DATA, playData);
                intent.putExtras(b);
                getActivity().startActivity(intent);
            }
        });

        return root;
    }

}
